package database;


import cn.beecp.BeeDataSource;
import cn.beecp.BeeDataSourceConfig;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Author: cc
 * @Description:
 */
public class BeePool {
    /**
     * 数据源
     */
    private static BeeDataSource dataSource;

    public static String dbUser = "root", dbPass = "root", dbIp = "localhost", dbName = "v079bt2";

    public static int dbPort = 3306;

    /**
     * 初始化数据源
     */
    private static void initBee() {
        // 因为dataSource是全局变量、默认初始化值为null
        if (dataSource == null) {
            BeeDataSourceConfig config = new BeeDataSourceConfig();
            config.setDriverClassName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://" + dbIp + ":" + dbPort + "/" + dbName + "?useSSL=false&useUnicode=true&characterEncoding=UTF8&failOverReadOnly=false&failOverReadOnly=false&rewriteBatchedStatements=true&zeroDateTimeBehavior=convertToNull";
            config.setJdbcUrl(url);
            config.setUsername(dbUser);
            config.setPassword(dbPass);
            config.setInitialSize(10);
            config.setMaxActive(100);
            try {
                dataSource = new BeeDataSource(config);
            } catch (Exception ex) {
                System.err.println(ex.getMessage());
            }
        }
    }

    /**
     * 通过数据源获取连接
     *
     * @return
     */
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    static {
        initBee();
    }

}
